package com.pactera.madp.cp.controller.oms.product;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.product.ProductGroupAddDto;
import com.pactera.madp.cp.api.dto.product.ProductGroupUpdateDto;
import com.pactera.madp.cp.api.entity.product.ProdEmployeeEntity;
import com.pactera.madp.cp.api.entity.product.ProductGroupEntity;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.ProdEmployeeService;
import com.pactera.madp.cp.service.ProductGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 产品分类
 *
 * @author kedongZhang
 * @date 2021/5/26 10:42
 */
@RestController
@AllArgsConstructor
@RequestMapping("/productgroup")
@Api(value = "productgroup", tags = "产品分类管理")
public class ProductGroupController {

    private ProductGroupService productGroupService;
    @Resource
    private ProdEmployeeService prodEmployeeService;

    /**
     * 新增分类
     *
     * @param productGroupAddDto 新增产品分类传输对象
     * @return 成功/失败
     */
    @PostMapping
    @ApiOperation(value = "新建分类")
    public R saveGroup(@Valid @RequestBody ProductGroupAddDto productGroupAddDto){
        //校验名称是否重复
        ProductGroupEntity groupEntity = productGroupService.getOne(Wrappers.<ProductGroupEntity>query().lambda()
                .eq(ProductGroupEntity::getName, productGroupAddDto.getName())
                .eq(ProductGroupEntity::getCorpId, CorpContextHolder.getCorpId()));
        if (groupEntity != null) {
            return R.failed(Boolean.FALSE,"新建失败,分类名称已存在");
        }
        return R.ok(productGroupService.saveGroup(productGroupAddDto));
    }

    /**
     * 根据id查询分类信息
     *
     * @param productGroupId 分类id
     * @return
     */
    @GetMapping("/{productGroupId}")
    @ApiOperation(value = "根据id查询分类信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="productGroupId",value="产品分类id",dataType="Integer")
    })
    public R getById(@PathVariable Integer productGroupId){
        return R.ok(productGroupService.getById(productGroupId));
    }

    /**
     * 获取分类列表
     *
     * @return List<ProductGroupVO>
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取分类列表")
    public R getGroupList(){
        return productGroupService.getGroupList();
    }

    /**
     * 编辑分类
     * @param productGroupUpdateDto 编辑产品分类传输对象
     * @return
     */
    @PutMapping
    @ApiOperation(value = "编辑分类")
    public R updateGroup(@Valid @RequestBody ProductGroupUpdateDto productGroupUpdateDto){
        return productGroupService.updateGroup(productGroupUpdateDto);
    }

    /**
     * 删除分类(同时删除分类下的产品)
     *
     * @param productGroupId 分类id
     * @return true/false
     */
    @DeleteMapping("/{productGroupId}")
    @ApiOperation(value = "删除分类(同时删除分类下的产品)")
    @ApiImplicitParams({
            @ApiImplicitParam(name="productGroupId",value="产品分类id",dataType="Integer")
    })
    public R deleteGroupById(@PathVariable Integer productGroupId){
        ProductGroupEntity productGroupEntity = productGroupService.getById(productGroupId);
        if (productGroupEntity.getName().equals("精选")) {
            return R.failed(Boolean.FALSE,"精选不能删除");
        }
        List<ProdEmployeeEntity> list = prodEmployeeService.list(Wrappers.<ProdEmployeeEntity>lambdaQuery()
                .eq(ProdEmployeeEntity::getCorpId, CorpContextHolder.getCorpId())
                .like(ProdEmployeeEntity::getProdRange, productGroupId));
        if(list.size()>0){
            return R.failed("该分类已被授权，请先取消授权");
        }
        return R.ok(productGroupService.deleteGroupById(productGroupId));
    }

    /**
     *
     *
     * @description:根据部门id获取分类
     * @param id 部门id
     * @return
     */
    @GetMapping("/dept")
    @ApiOperation(value = "根据部门id获取分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "部门id")
    })
    public R getProductGroupByDept(@RequestParam Integer id){
        return productGroupService.getProductGroupByDept(id);
    }

}
